package com.yc.system.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.yc.framework.baseClass.R;
import com.yc.sssmpg.dto.ProductDto;
import com.yc.system.dto.Filterbook;
import com.yc.system.dto.esmodel.BookInfo;
import com.yc.system.entity.Book;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.yc.system.entity.BookCartLike;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * 图书信息表 Mapper 接口
 * </p>
 *
 * @author 1850140986@qq.com
 * @since 2022-12-03
 */
@Mapper
public interface BookMapper extends BaseMapper<Book> {

//    @Select("select book_id from t_book ${ew.customSqlSegment} add cover_photo = #{reqFullFileName}")
@Select("select book_id from t_book where cover_photo = #{reqFullFileName}")
    int getBookIdByCoverPhoto(String reqFullFileName);

//    @Update("update t_book set cover_photo=#{reqFullFileName} ${ew.customSqlSegment} add book_id = #{bookId}")
@Update("update t_book set cover_photo=#{reqFullFileName} where book_id = #{bookId}")
    int updateCoverPhotoByBookId(int bookId, String reqFullFileName);

@Select("select cover_photo from t_book where book_id=#{bookId}")
    String getCoverPhotoByBookId(int bookId);

    @Update("update t_book set book_name=#{bookName},type_id=#{typeId},user_id=#{userId},publisher=#{publisher},author=#{author},f_price=#{fPrice},n_price=#{nPrice},discount=#{discount}," +
        "depreciation=#{depreciation},pub_date=#{pubDate},keyword=#{keyword},book_name=#{bookName},isbn=#{isbn},brief=#{brief},quantity=#{quantity},sell_num=#{sellNum},like_num=#{likeNum}," +
        "book_status=#{bookStatus},added_time=#{addedTime} where book_id=#{bookId}")
    int updateBybookId(Book model);

    @Select("select t_book.*,truncate(t_book.n_price*t_book.discount/10,2) as price from t_book where user_id=#{userId} and deleted=0")
    List<Book> selectBooksByUserId(int userId);

    @Select("select t_book.*,truncate(t_book.n_price*t_book.discount/10,2) as price from t_book where book_status=5 and deleted=0")
    List<Book> getSwiper();

    @Select("select t_book.*,truncate(t_book.n_price*t_book.discount/10,2) as price from t_book where book_id=#{bookId}")
    Book getBookByBookId(int bookId);

//    分页例题
//    @Select("select * from t_product   ${ew.customSqlSegment}")
//    IPage<ProductDto> listVoXxx(IPage<ProductDto> page, @Param(Constants.WRAPPER) QueryWrapper<ProductDto> queryWrapper, @Param("status") int status);
    @Select("select t_book.*,truncate(t_book.n_price*t_book.discount/10,2) as price from t_book where user_id=#{userId} and deleted=0")
    List<Book> getBooksByPageByUserId(IPage<Book> page, @Param(Constants.WRAPPER) QueryWrapper<Book> queryWrapper, @Param("status") int status,int userId);

    @Select("select count(book_id) from t_book where user_id=#{userId} and deleted=0")
    int getTotal(int userId);

    @Select("select t_book.*,truncate(t_book.n_price*t_book.discount/10,2) as price from t_book where deleted=0")
    List<Book> getBooksByPageRandom(IPage<Book> page, @Param(Constants.WRAPPER) QueryWrapper<Book> queryWrapper, @Param("status") int status);

    @Select("select t_book.*,truncate(t_book.n_price*t_book.discount/10,2) as price from t_book where  deleted=0 and type_id in (select type_id from t_book_type where p_id=#{pId})")
    List<Book> getBooksByPId(IPage<Book> page, @Param(Constants.WRAPPER) QueryWrapper<Book> queryWrapper, @Param("status") int status,Integer pId);

    @Select("select t_book.*,truncate(t_book.n_price*t_book.discount/10,2) as price  from t_book where type_id = #{typeId} and deleted=0")
    List<Book> getBooksByPageAndTypeId(IPage<Book> page, @Param(Constants.WRAPPER) QueryWrapper<Book> queryWrapper, @Param("status") int status,Integer typeId);

//    typeId,userId,publisher,author,n_price,，keyword，isbn，sellNum，like_num
//    @Select("select * from t_book where publisher like '%#{publisher}%' and author like '%#{author}%' and keyword like '%#{keyword}%' and isbn like '%#{isbn}%'")
//    List<Book> filterbook(Book mode,IPage<Book> page, @Param(Constants.WRAPPER) QueryWrapper<Book> queryWrapper, @Param("status") int status);

    @Select("select * from t_book where publisher LIKE CONCAT('%',#{publisher},'%')  and author like CONCAT('%',#{author},'%') and keyword like CONCAT('%',#{keyword},'%') and isbn like CONCAT('%',#{isbn},'%') and deleted=0")
    List<Book> filterbook(Book mode);

    @Select("select book.*,user.username as issuerName,type.type_name as typeName,type.p_id as pId,truncate(book.n_price*book.discount/10,2) as price " +
            "from t_book as book join t_user as user join t_book_type as type " +
            "on book.user_id=user.user_id and book.type_id=type.type_id and book.deleted=0")
    List<BookInfo> booksInfoForES();

    @Select("select t_book.*,truncate(t_book.n_price*t_book.discount/10,2) as price from t_book where deleted=0 and book_id in (select book_id from t_book_cart_like where cl_status in (1,3) and user_id=#{userId} and deleted=0)")
    List<BookInfo> getlikeByPageByUserId(IPage<Book> page, @Param(Constants.WRAPPER) QueryWrapper<Book> queryWrapper, @Param("status") int status,int userId);

    @Select("select count(book_id) from t_book where deleted=0 and  book_id in (select book_id from t_book_cart_like where cl_status in (1,3) and user_id=#{userId} and deleted=0)")
    int getlikeTotal(Integer userId);

    @Update("update t_book set deleted=1 where book_id = #{bookId}")
    int deleteBybookId(int bookId);

    @Update("update t_book set quantity=quantity-#{cartNum} where book_id = #{bookId}")
    int changeQuantity(BookCartLike cart);
}
